<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">

          <el-form-item label="" prop="orderSn">
            <el-input
              v-model="queryParams.orderSn"
              placeholder="请输入订单号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="借款人"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="userPhone">
            <el-input
              v-model="queryParams.userPhone"
              placeholder="手机号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="applyCycle">
            <el-input
              v-model="queryParams.applyCycle"
              placeholder="周期"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="applyType">
            <el-select
              v-model="queryParams.applyType"
              placeholder="贷款状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in applyTypeList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="userId">
            <el-select
              v-model="queryParams.userId"
              placeholder="审核人"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="审核状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in statusList"
                :key="item.label"
                :label="item.value"
                :value="item.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="申请开始日期"
              end-placeholder="申请结束日期"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="">
            <el-date-picker
              v-model="dateRange1"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="审核开始日期"
              end-placeholder="审核结束日期"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="applyList">
          <el-table-column type="index" label="序号" width="50" align="center"/>
          <el-table-column label="贷款单号" align="center" prop="orderSn"/>
          <el-table-column label="手机号" align="center" prop="userPhone"/>
          <el-table-column label="借款人" align="center" prop="userName"/>
          <el-table-column label="借款金额" align="center" prop="realApplyMoney"/>
          <el-table-column label="产品周期" align="center" prop="applyCycle"/>
          <el-table-column label="审核结果" align="center" prop="status">
            <template slot-scope="scope">
              <span v-for="item in statusList" v-if="scope.row.status == item.label">{{ item.value }}</span>
            </template>
          </el-table-column>
          <el-table-column label="申请时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="审核时间" align="center" prop="examineTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.examineTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            fixed="right"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleUpdate(scope.row)"
              >订单回收
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
              >
                <router-link :to="'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId" class="link-type">
                  <span>查看</span>
                </router-link>
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {listFailApply,listExamineUser,recoveryApply} from "@/api/app/apply";
export default {
  name: "Apply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      userList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      dateRange1: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        status: undefined,
        applyType: undefined,
        userPhone: undefined,
        userName: undefined,
        orderSn: undefined
      },
      statusList: [
        {
          label: '1',
          value: '初审拒绝'
        },
        {
          label: '3',
          value: '复审拒绝'
        },
        {
          label: '99',
          value: '机审拒绝'
        }
      ],

      applyTypeList: [
        {
          value: '0',
          label: '首借'
        },
        {
          value: '1',
          label: '复借'
        }
      ],
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getExamineUserList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.dateRange && Array.isArray(this.dateRange)){
        this.queryParams.createStartTime = this.dateRange[0];
        this.queryParams.createEndTime = this.dateRange[1];
      }
      if (this.dateRange1 && Array.isArray(this.dateRange1)){
        this.queryParams.examineStartTime = this.dateRange1[0];
        this.queryParams.examineEndTime = this.dateRange1[1];
      }
      listFailApply(this.queryParams).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getExamineUserList() {
      listExamineUser().then(response => {
          this.userList = response.data;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        applyMoney: 0,
        manageMoney: 0,
        examineMoney: 0,
        serviceMoney: 0,
        cycle: 0,
        realMoney: 0,
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.dateRange1 = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id;
    //   getApply(id).then(response => {
    //     this.form = response.data;
    //     this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    //     this.open = true;
    //     this.title = "修改";
    //   });
    // },


    /** 提交按钮 */
    submitForm: function () {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 订单回收 */
    handleUpdate(row) {
      this.$modal.confirm('是否回收？').then(function () {
        return recoveryApply(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    // handleMoney(){
    //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    // }
  }
};
</script>
